package com.cht.mailGenerator;

import java.util.ArrayList;
import java.util.Properties;

import javax.mail.Message.RecipientType;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import com.cht.commonUtil.structure.InfoStruc;
import com.cht.commonUtil.widgets.Widgets;

public class MailSend {


	public String sendMailMain(String fromAddr, String fromAddrName, ArrayList<InfoStruc> recipients, String subject, String msgBody) {

		String res = "";

		final Properties prop = new Properties();
		final Session session = Session.getDefaultInstance(prop, null);

		prop.put("mail.smtp.host", "10.160.1.1");

		try {
			MimeMessage msg = new MimeMessage(session);

			msg.setSubject(new String(subject.getBytes("big5"), "big5"), "big5");
			InternetAddress from = new InternetAddress(fromAddr);
			from.setPersonal(fromAddrName, "big5");
			msg.setFrom(from);

			InternetAddress to = new InternetAddress();
			for (InfoStruc rec : recipients) {
				to.setAddress(rec.getId());
				to.setPersonal(rec.getName(), "big5");
				msg.addRecipient(RecipientType.TO, to);
			}

			Multipart mp = new MimeMultipart();

			MimeBodyPart mbp1 = new MimeBodyPart();
			mbp1.setContent(msgBody, "text/html;charset=UTF-8");
			mp.addBodyPart(mbp1);
			msg.setContent(mp);
			Transport.send(msg);

		} catch (Exception e) {
			res = Widgets.errLog(e);
		}
		return res;
	}

}
